package com.xbongbong.pro.statistic.pojo.dto;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.pro.domain.entity.ChartEntity;
import com.xbongbong.pro.statistic.enums.ChartTypeEnum;
import com.xbongbong.pro.statistic.enums.StatisticsTypeEnum;
import com.xbongbong.pro.statistic.service.ChartService;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.Arrays;

/**
 * @author 梁鲁江
 * @version v1.0
 * @date 2019/1/18 23:03
 * @since v1.0
 */
@Setter
@Getter
@ToString
public class ChartValueSaveEntity extends ChartEntity {
    private static final long serialVersionUID = 2421871632930568588L;
    /**
     * 报表名称
     */
    @NotNull(message = "指标名称不能为空")
    @Size(max=20,   message ="指标名称长度不可大于20" )
    private String name;
    /**
     * 指标/图表图标
     */
    @NotNull(message = "图标不能为空", groups = {ChartService.class})
    private String icon;
    /**
     * 颜色
     */
    @NotNull(message = "请选择颜色", groups = {ChartService.class})
    private String color;
    /**
     * 主表数据来源，只能有一个，代表主驱动表 格式为：{"appId":1,"menuId":2,"linkAttr":"text_1"}
     */
    @NotEmpty(message = "数据来源不能为空")
    private JSONObject driverSources;
    /**
     * 归属人字段，{"name":"","attr":"userId"}。按照哪一个归属字段统计（如创建人、签订人等）
     */
    @NotEmpty(message = "归属人不能为空")
    private JSONObject belongAttr;
    /**
     * 统计时间
     * 时间字段，{"name":"","attr":""}。按照哪一个时间范围字段进行统计（如创建时间、自定义的时间类型字段）
     */
    @NotEmpty(message = "统计时间不能为空")
    private JSONObject timeAttr;
    /**
     * 统计方式
     */
    @NotNull(message = "统计方式不能为空")
    private Integer statisticsWay;
    /**
     * 单位（系统指标的单位可默认，如合同金额为“元”）
     */
    @NotNull(message = "单位不能为空")
    private String unit;
    /**
     * 当单位选择自定义的时候，用来存储自定义的值
     */
    private String  unitValue;
    /**
     * 分类id
     */
    @NotNull(message = "分类id不能为空")
    private Long categoryId;

    /**
     * 高级过滤条件
     * 筛选条件 格式：[{}]
     */
    private JSONArray filter;
    /**
     * 统计字段不能为空
     */
    @NotNull(message = "统计字段不能为空")
    private JSONObject sourceAttr;

    /**
     * 穿透展示字段[{"name":"","attr":""}]
     */
    private JSONArray penetrateAttr;

    /**
     * [{property: "dept", editable: 1, id: 5, name: "研发部"},
     * {property: "role", editable: 1, id: "2", name: "角色2"},
     * {property: "user", editable: 1, id: "1", name: "张杰"}]
     * Pk榜范围设置参与的人员
     */
    private JSONArray joinRule;

    /**
     * [{property: "dept", editable: 1, id: 5, name: "研发部"},
     * {property: "role", editable: 1, id: "2", name: "角色2"},
     * {property: "user", editable: 1, id: "1", name: "张杰"}]
     * Pk榜范围设置不参与的人员
     */
    private JSONArray removeRule;

    /**
     * [{property: "dept", editable: 1, id: 5, name: "研发部"},
     * {property: "role", editable: 1, id: "2", name: "角色2"},
     * {property: "user", editable: 1, id: "1", name: "张杰"}]
     * Pk榜可见权限范围
     */
    private JSONArray scopeRule;

    /**
     * Pk榜可见范围设置，可见数据权限类型，0，数据权限，1，全部数据
     */
    private Integer powerType;

    public ChartValueSaveEntity() {
        super.chartType = ChartTypeEnum.VALUE.getCode();
        super.showTypes = new JSONArray(Arrays.asList(ChartTypeEnum.VALUE.getCode()));
        super.statisticsType = StatisticsTypeEnum.CUSTOM_STATISTIC.getCode();
        super.publish = 0;
        super.enable = 1;
    }

    public void customPkSaveFormat() {
        super.chartType = ChartTypeEnum.PK_CUSTOM.getCode();
        super.showTypes = new JSONArray(Arrays.asList(ChartTypeEnum.PK_CUSTOM.getCode()));
        super.statisticsType = StatisticsTypeEnum.CUSTOM_STATISTIC.getCode();
        super.publish = 0;
        super.enable = 1;
    }
}
